package com.smw.controller;

import com.smw.dao.ProductDao;
import com.smw.entity.Product;
import com.smw.entity.Type;
import com.smw.entity.User;


import com.smw.sendemail.SendEmail;
import com.smw.service.CodeService;
import com.smw.service.TypeService;
import com.smw.service.ULoginService;
import com.smw.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;



@Controller
@RequestMapping("/user")
public class UserController {

















    @Autowired
    private CodeService codeService;
    @Autowired
    private ULoginService uLoginService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private ProductDao productDao;

    @RequestMapping("/login")
    @ResponseBody
    public String login(User user, Model model){
        String token= JwtUtil.JwtToken(user.getUserName(),user.getPassWord());
        System.out.println("进入控制层");
        User u=uLoginService.Login(user.getUserName(),user.getPassWord());
        if(u!=null){
            model.addAttribute("modelUsers",u);
            return token;
        }
        else{
            return null;}
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public String checkName(String userName){
            User u = uLoginService.findUserByName(userName);

            if (u == null) {
                return "1";
            } else {
                return "0";
            }


    }
////    @RequestMapping("/toSuccess")
////    public String success(Model model){
////        List<Type>t=typeService.selectAllType();
////        model.addAttribute("type",t);
////        return "main";
////    }
////    @RequestMapping("/queryProjectByTabs")
////    public String queryProjectByTabs(Integer TypeId,Model model){
////        List<Product>p=productDao.queryProductByTabs(TypeId);
////        model.addAttribute("product",p);
////        return "main";
////    }
//
    @RequestMapping("/reg")
    public String reg(User user){
        if (user.getEmailCode().equals(codeService.findCodeByKey(user.getEmail()))){
            uLoginService.addUser(user);
                return "login";

        }else{
            return "reg";
        }


    }
    @RequestMapping("/lreg")
    public String lreg(User user){
        System.out.println(user);
        return "reg";
    }
    @RequestMapping("/relogin")
    public String ReIndex(User user){
        System.out.println(user);
        return "reg";
    }
    @RequestMapping("/getEmailCode")
    @ResponseBody
    public String getEmailCode(String email, HttpServletRequest request) {

        StringBuffer sbCode = new StringBuffer();
        System.out.println("进入邮箱验证码");
        Random r = new Random();
        for (int i = 0; i < 6; i++) {
            int txt = r.nextInt(10);
            String str = String.valueOf(txt);
            sbCode.append(str);
        }
        //将验证码存到session中，方便登录时来获取这个验证去校验用户登陆时所输入的验证码是否正确
        request.getSession().setAttribute("code", sbCode.toString());
        //拼接邮件内容
        String code=sbCode.toString();
        String msg = "验证码为：" + sbCode.toString() + ",30分钟内有效";
        System.out.println("验证码为"+msg);
        codeService.saveCode(email,code);
        boolean flag= SendEmail.sendMail(email, msg);
        if(flag){
            return "1";
        }else{
            return "0";
        }

    }


}
